﻿using MVP;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WinForms.Bku;

namespace WinForms
{
    public partial class MainForm : Form, IMainFormView
    {
        private IList<Control> _controls;
        private MainFormPresenter _presenter;

        public MainForm()
        {
            // Put the following code before InitializeComponent()
            // Sets the culture to ID
            Thread.CurrentThread.CurrentCulture = new CultureInfo("id-ID");
            // Sets the UI culture to ID
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("id-ID");

            InitializeComponent();
            _controls = new List<Control>();
            _presenter = new MainFormPresenter(this);

            ValidateLicense();
        }

        private void ValidateLicense()
        {
            if (!_presenter.IsProductActivated())
            {
                FeatureEnabled(false);
            }
        }

        private void FeatureEnabled(bool isEnabled)
        {
            openToolStripMenuItem.Enabled = isEnabled;
            saveToolStripMenuItem.Enabled = isEnabled;
            profilToolStripMenuItem.Enabled = isEnabled;
            printToolStripMenuItem.Enabled = isEnabled;
            this.Refresh();
        }

        #region IMainFormView Members

        public event EventHandler Initialize;

        #endregion

        private void add(Control control)
        {
            foreach (Control activeControl in _controls)
            {
                mainPanel.Controls.Remove(activeControl);
            }
            _controls.Clear();
            _controls.Add(control);
            mainPanel.Controls.Add(control);
            _presenter.States["ActiveView"] = control;
        }

        private void bukuKasUmumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String[] period = SelectPeriodBulanBox.Show();
            if (period != null)
            {
                BukuKasUmum bukuKasUmum = new BukuKasUmum(period[0], period[1]);
                bukuKasUmum.Dock = DockStyle.Fill;
                add(bukuKasUmum);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "Excel Workbook (*.xlsx)|*xlsx";
            saveDialog.Title = "Simpan sebagai file Excel";
            saveDialog.ShowDialog();

            if (saveDialog.FileName != "")
            {
                String filename = saveDialog.FileName;
                if (_presenter.States.ContainsKey("ActiveView"))
                {
                    if (!_presenter.IsProfileSet())
                    {
                        MessageBox.Show("Profile harus diisi terlebih dahulu. Pilih Menu > Profil.");
                        return;
                    }
                    Control activeView = (Control)_presenter.States["ActiveView"];
                    if (activeView is BukuKasUmum)
                    {
                        ((BukuKasUmum)activeView).GetPresenter().ExportToExcel(filename, ((BukuKasUmum)activeView).LppbopDataSet);
                    }
                    else if (activeView is LaporanBulanan)
                    {
                        ((LaporanBulanan)activeView).GetPresenter().ExportToExcel(filename, ((LaporanBulanan)activeView).LppbopDataSet);
                    }
                    else if (activeView is LaporanSemester)
                    {
                        ((LaporanSemester)activeView).GetPresenter().ExportToExcel(filename, ((LaporanSemester)activeView).LppbopDataSet);
                    }
                    else if (activeView is LaporanTriwulan)
                    {
                        ((LaporanTriwulan)activeView).GetPresenter().ExportToExcel(filename, ((LaporanTriwulan)activeView).LppbopDataSet);
                    }
                }
            }
        }

        private void laporanBulananToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String[] period = SelectPeriodBulanBox.Show();
            if (period != null)
            {
                LaporanBulanan laporanBulanan = new LaporanBulanan(period[0], period[1]);
                laporanBulanan.Dock = DockStyle.Fill;
                add(laporanBulanan);
            }
        }

        private void laporanTriwulanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String[] period = SelectPeriodTriwulanBox.Show();
            if (period != null)
            {
                LaporanTriwulan laporanTriwulan = new LaporanTriwulan(period[0], period[1]);
                laporanTriwulan.Dock = DockStyle.Fill;
                add(laporanTriwulan);
            }
        }

        private void laporanSemesterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String[] period = SelectPeriodSemesterBox.Show();
            if (period != null)
            {
                LaporanSemester laporanTriwulan = new LaporanSemester(period[0], period[1]);
                laporanTriwulan.Dock = DockStyle.Fill;
                add(laporanTriwulan);
            }
        }

        private void profilToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Profil().ShowDialog();
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_presenter.States.ContainsKey("ActiveView"))
            {
                PrintDialog printDialog = new PrintDialog();
                printDialog.PrinterSettings = new PrinterSettings();

                if (printDialog.ShowDialog() == DialogResult.OK)
                {
                    Control activeView = (Control)_presenter.States["ActiveView"];
                    if (activeView is BukuKasUmum)
                    {
                        ((BukuKasUmum)activeView).GetPresenter().Print(((BukuKasUmum)activeView).LppbopDataSet, printDialog.PrinterSettings);
                    }
                    else if (activeView is LaporanBulanan)
                    {
                        ((LaporanBulanan)activeView).GetPresenter().Print(((LaporanBulanan)activeView).LppbopDataSet);
                    }
                    else if (activeView is LaporanSemester)
                    {
                        ((LaporanSemester)activeView).GetPresenter().Print(((LaporanSemester)activeView).LppbopDataSet);
                    }
                    else if (activeView is LaporanTriwulan)
                    {
                        ((LaporanTriwulan)activeView).GetPresenter().Print(((LaporanTriwulan)activeView).LppbopDataSet);
                    }
                }
            }
        }

        private void aktivasiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AktivasiForm.AktivasiDialogBox.Show())
            {
                FeatureEnabled(true);
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "Excel Workbook (*.xlsx)|*xlsx";
            saveDialog.Title = "Simpan sebagai file Excel";
            saveDialog.ShowDialog();

            if (saveDialog.FileName != "")
            {
                String filename = saveDialog.FileName;
                Common.ExcelUtil.Create(filename);
            }
        }
    }
}
